package cn.usian.controller;

import cn.usian.pojo.Student;
import cn.usian.service.StudentService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;


    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }
    /**
     * 查询
     * @param model
     * @return
     */

    @RequestMapping("findAll")
    public String findAll(Model model) {
        List<Student> list = studentService.findAll();
        model.addAttribute("list", list);
        return "student";
    }

    /**
     * 添加跳转
     * @return
     */
    @GetMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    /**
     * 添加
     * @param student
     * @return
     */
    @PostMapping("add")
    public String add(Student student){
        studentService.add(student);
        return "redirect:/findAll";
    }

    /**
     * 删除
     * @param uid
     * @return
     */
    @GetMapping("delete")
    public String delete(Integer uid){
        studentService.delete(uid);
        return "redirect:/findAll";
    }
    /**
     * 修改回显
     * @return
     */
    @GetMapping("findByUid")
    public String findByUid(Model model,Integer uid){
        Student student=studentService.findByUid(uid);
        model.addAttribute("student",student);
        return "update";
    }

    /**
     * 修改
     * @param student
     */
    @PostMapping("update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:/findAll";
    }
}
